package com.iyao_app.im.adapter;

import android.content.Context;
import android.net.Uri;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.facebook.drawee.backends.pipeline.Fresco;
import com.facebook.drawee.backends.pipeline.PipelineDraweeController;
import com.facebook.drawee.view.SimpleDraweeView;
import com.facebook.imagepipeline.common.ResizeOptions;
import com.facebook.imagepipeline.request.ImageRequest;
import com.facebook.imagepipeline.request.ImageRequestBuilder;
import com.iyao_app.R;
import com.iyao_app.entry.MyInterestBean;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by xue on 2017/11/28.
 */

public class AttentionInterestMyAdapter extends RecyclerView.Adapter<AttentionInterestMyAdapter.InterestMy> {

    private Context context;
    private RecyclerView recyclerView;
    private ONCLICK onclick;
    public List<MyInterestBean> list = new ArrayList<MyInterestBean>();
    private LayoutInflater inflater;
    public String friendId;
    private MyClickListener mListener;

    public AttentionInterestMyAdapter(Context context, RecyclerView recyclerView, List<MyInterestBean> list, ONCLICK onclick,MyClickListener mListener) {
        this.context = context;
        this.recyclerView = recyclerView;
        this.onclick = onclick;
        this.list = list;
        this.mListener = mListener;
        inflater = LayoutInflater.from(context);
    }



    public interface ONCLICK {
        public void onclick(int position);
    }

    /**
     * 用于回调的抽象类
     * @author xue
     */
    public static abstract class MyClickListener implements View.OnClickListener {
        /**
         * 基类的onClick方法
         */
        @Override
        public void onClick(View v) {
            myOnClick((Integer) v.getTag(), v);
        }
        public abstract void myOnClick(int position, View v);
    }

    @Override
    public InterestMy onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = inflater.inflate(R.layout.item_interestmy, null);
        return new InterestMy(view);
    }

    @Override
    public void onBindViewHolder(final InterestMy holder, final int position) {

        holder.attention.setOnClickListener(mListener);
        holder.attention.setTag(position);

        holder.friend_name.setText(list.get(position).getNickName());
//        Glide.with(context).load(list.get(position).()).centerCrop().into(holder.head_portrait);
        try {
            Uri uri = Uri.parse(list.get(position).getFavicon());
            ImageRequest request = ImageRequestBuilder.newBuilderWithSource(uri)
                    .setResizeOptions(new ResizeOptions(100, 100))
                    .build();
            PipelineDraweeController controller = (PipelineDraweeController) Fresco.newDraweeControllerBuilder()
                    .setOldController(holder.head_portrait.getController())
                    .setImageRequest(request)
                    .build();
            holder.head_portrait.setController(controller);
        } catch (Exception e) {
            e.printStackTrace();
        }

        int sex = list.get(position).getSex();
        if (sex != 0){
            if (sex == 1){
                Glide.with(context).load(R.mipmap.ic_boy).centerCrop().into(holder.friend_sex);
            }else if (sex == 2){
                Glide.with(context).load(R.mipmap.ic_girl).centerCrop().into(holder.friend_sex);
            }
        }else {

        }
    }

    @Override
    public int getItemCount() {
        if (list != null) {
            return list.size();
        }
        return 0;
    }

    class InterestMy extends RecyclerView.ViewHolder {

        RelativeLayout rl_attention;
        SimpleDraweeView head_portrait;
        TextView friend_name;
        TextView friend_message;
        ImageView friend_sex;
        Button attention;

        public InterestMy(View itemView) {
            super(itemView);
            rl_attention = itemView.findViewById(R.id.rl_attention);
            head_portrait = itemView.findViewById(R.id.head_portrait);
            friend_name = itemView.findViewById(R.id.friend_name);
            friend_message = itemView.findViewById(R.id.friend_message);
            friend_sex = itemView.findViewById(R.id.friend_sex);
            attention = itemView.findViewById(R.id.btn_attention);
            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    int i = recyclerView.getChildPosition(view);
                    onclick.onclick(i);
                }
            });
        }
    }
}

